import httplib2
import json
from .config import key

# 高德天气接口
def gaode_weather(key: str, code: str):
    url = "https://restapi.amap.com/v3/weather/weatherInfo?"
    pram = "key="+key+"&city="+code+"&extensions=all"
    http = httplib2.Http()
    response, content = http.request(url + pram, 'GET')
    return content

# 高德天气api
async def request_gaode(code: str) -> None:

    print("高德")
    week = ('零', '一', '二', '三', '四', '五', '六', '日')
    content = gaode_weather(key, code)
    content = content.decode('utf-8')
    result = json.loads(content)

    msg = "来自焘焘对的关怀！\n"
    city = "温馨提示： " + result['forecasts'][0]['province'] + result['forecasts'][0]['city']+"   "

    date = result['forecasts'][0]['casts'][0]['date'] + "  星期" + week[
        (int)(result['forecasts'][0]['casts'][0]['week'])]+"\n天气："

    # 天气情况解析
    dayweather = result['forecasts'][0]['casts'][0]['dayweather']
    nightweather = result['forecasts'][0]['casts'][0]['nightweather']
    if dayweather == nightweather:
        weather = dayweather
    else:
        weather = dayweather + "转" + nightweather
    weather = weather+"，"
    # 温度解析
    daytemp = result['forecasts'][0]['casts'][0]['daytemp']
    nighttemp = result['forecasts'][0]['casts'][0]['nighttemp']
    if daytemp < nighttemp:
        temp = daytemp + "-" + nighttemp + "℃"
    else:
        temp = nighttemp + "-" + daytemp + "℃"
    temp = temp + "，"
    # 风向和风力
    daywind = result['forecasts'][0]['casts'][0]['daywind']
    daypower = result['forecasts'][0]['casts'][0]['daypower']
    daywp = "风向：" + daywind + "  " + daypower + "级"

    return msg + city + date + weather+ temp + daywp